package com.github.butid.data;

import com.alipay.remoting.ConnectionEventType;
import com.github.butid.data.rpc.common.BoltServer;
import com.github.butid.data.rpc.common.CONNECTEventProcessor;
import com.github.butid.data.rpc.common.DISCONNECTEventProcessor;
import com.github.butid.data.rpc.common.ButServerUserProcessor;

/**
 * @author wangjing.dc@qq.com
 * @since 2020/8/20
 */
public class RpcNettyServer {

    private BoltServer server;
    private ButServerUserProcessor serverUserProcessor       = new ButServerUserProcessor();
    private CONNECTEventProcessor serverConnectProcessor    = new CONNECTEventProcessor();
    private DISCONNECTEventProcessor serverDisConnectProcessor = new DISCONNECTEventProcessor();

    public RpcNettyServer(Integer poart){
        // 1. create a Rpc server with port assigned
        server = new BoltServer(poart);
        // 2. add processor for connect and close event if you need
        server.addConnectionEventProcessor(ConnectionEventType.CONNECT, serverConnectProcessor);
        server.addConnectionEventProcessor(ConnectionEventType.CLOSE, serverDisConnectProcessor);
        // 3. register user processor for client request
        server.registerUserProcessor(serverUserProcessor);
    }

    public void start(){
        server.start();
    }

    public void stop(){
        server.stop();
    }
}
